package com.yztc.thinkpad.mandemo.cooladapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.thinkpad.mandemo.CoolWebViewActivity;
import com.yztc.thinkpad.mandemo.R;
import com.yztc.thinkpad.mandemo.coolbean.CoolDataBean;
import com.yztc.thinkpad.mandemo.coolinterface.ICoolRecyclerViewItemClick;

import java.util.List;

/**
 * Created by Administrator on 2016/10/8.
 */
public class Cool_RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private static final int TYPE_ITEM =0;  //普通Item View
    private static final int TYPE_FOOTER = 1;  //顶部FootView
    private static final int TYPE_LAST=2;
    //上拉加载更多状态-默认为0
    public static int load_more_status=0;
    //上拉加载更多
    public static final int  PULLUP_LOAD_MORE=0;
    //正在加载中
    public static final int  LOADING_MORE=1;

    private List<CoolDataBean> list;
    private ICoolRecyclerViewItemClick mCoolRecyclerViewItemClick;

    public Cool_RecyclerAdapter(Context context,List<CoolDataBean> list){
        this.context=context;
        this.list=list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //进行判断显示类型，来创建返回不同的View
        if(viewType==TYPE_ITEM){
            MyViewHolder holder=new MyViewHolder(LayoutInflater.from(context)
                    .inflate(R.layout.cool_list_item,parent,false));
            return holder;
        }else if(viewType==TYPE_FOOTER){
            View foot_view=LayoutInflater.from(context).inflate(R.layout.recycler_load_more_layout,parent,false);
            //这边可以做一些属性设置，甚至事件监听绑定
//            foot_view.setBackgroundColor(Color.RED);
            FootViewHolder footViewHolder=new FootViewHolder(foot_view);
            return footViewHolder;
        }else if(viewType==TYPE_LAST){

        }
        return null;
    }
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        //点击item跳转到CoolWebViewActivity
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, CoolWebViewActivity.class);
                intent.putExtra("title", list.get(position).getTitle());
                intent.putExtra("id", list.get(position).getDress_school_id() + "");
                intent.putExtra("image",list.get(position).getImage());
                context.startActivity(intent);
            }
        });
        //item的布局控件
        if(holder instanceof MyViewHolder) {
            ((MyViewHolder)holder).tv_cool_title.setText(list.get(position).getTitle());
            ((MyViewHolder)holder).tv_cool_clickCount.setText(list.get(position).getClickCount()+"");
            Glide.with(context).load(list.get(position).getImage())
                    .centerCrop()
                    .placeholder(R.mipmap.default_banner)
                    .error(R.mipmap.default_faile_image)
                    .into( ((MyViewHolder)holder).iv_cool_image);
        }else if(holder instanceof FootViewHolder){
            FootViewHolder footViewHolder=(FootViewHolder)holder;
//            Log.e("tag","%%%%%%%%%%%%%%%%%%%"+list.size());
            switch (load_more_status){
                case PULLUP_LOAD_MORE:
                    footViewHolder.tv_jiazai.setText("正在加载更多...");
                    Glide.with(context).load(R.mipmap.jiazai).into( footViewHolder.iv_jiazai);
                    break;
                case LOADING_MORE:
                    footViewHolder.tv_jiazai.setText("已经没有更多了...");
                    Glide.with(context).load(R.mipmap.jiazai).into( footViewHolder.iv_jiazai);
                    break;
            }
        }else {

        }
    }
    //注意事项：list.size()+1
    @Override
    public int getItemCount() {
        return list.size()+1;
    }
//注意事项
    @Override
    public int getItemViewType(int position) {

        if (position+1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    //item布局控件
    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView tv_cool_title,tv_cool_clickCount;
        ImageView iv_cool_image;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv_cool_image= (ImageView)itemView.findViewById(R.id.iv_cool_image);
            tv_cool_title= (TextView) itemView.findViewById(R.id.tv_cool_title);
            tv_cool_clickCount= (TextView) itemView.findViewById(R.id.tv_cool_clickCount);
        }
    }
    //上拉加载布局控件
    class FootViewHolder extends RecyclerView.ViewHolder{
        TextView tv_jiazai;
        ImageView iv_jiazai;
        public FootViewHolder(View itemView) {
            super(itemView);
            tv_jiazai= (TextView)itemView.findViewById(R.id.tv_jiazai);
            iv_jiazai= (ImageView)itemView.findViewById(R.id.iv_jiazai);

        }
    }
    class LastViewHolder extends RecyclerView.ViewHolder{
    TextView tv_last;
        public LastViewHolder(View itemView) {
            super(itemView);
            tv_last= (TextView) itemView.findViewById(R.id.tv_last);
        }
    }
    //分页添加到list集合
    public void rush(List<CoolDataBean>mlist,boolean isClear){
        if(isClear){
            list.clear();
        }
        list.addAll(mlist);
        notifyDataSetChanged();
    }
}
